package com.ys.controller.web.purchase;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.purchase.TcgPurchaseByOrder;
import com.ys.entity.purchase.TcgPurchaseByOrderSon;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 采购订单
 */
@Controller
@RequestMapping("/web/purchase/tcgPurchaseByOrder")
public class WebTcgPurchaseByOrderController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcgPurchaseByOrderController.class);

	@Autowired
	private CommonApiService<TcgPurchaseByOrder> commonApiService;

    private final Long MENU_ID = MenuId.TCG_PurchaseByOrder;
    private final Long MENU_ID1 = MenuId.TCG_PurchaseOrderWc;


    /*
     * 编辑
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            Map<String, Object> extraParam = getExtraParam(bid);

            //获取列表信息
            List<Map<String, Object>> itemList = getItemListByPid(Long.parseLong(extraParam.get("id").toString()));

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,itemList);

            return commonApiService.toDetail(model,Long.parseLong(extraParam.get("id").toString()), new TcgPurchaseByOrder(), MENU_ID, extraParam, subGridConfig);

        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    public Map<String,Object> getExtraParam(String bid){
        Map<String,Object> extraParam = new TcgPurchaseByOrder()


                .addLeftJoin("TCG_PurchaseByPlan B ON B.Bid = A.PlanBid")//采购计划表
                .addField("B.Vgsbm AS vgsbm")//运营公司编码
                .addField("B.Remark AS remark")//计划单备注

                .addLeftJoin("THR_OperateCompanyApply C ON C.Vgsbm = A.Vgsbm ")//运营公司管理表
                .addField("C.Vgsjc AS vgsjc")//采购公司简称
                .addField("C.Vgsmc AS vgsmc")//采购公司名称

                .addLeftJoin("TCG_PurchasePlanGoods D ON D.Id = A.PurchasePlanGoodsId")//采购计划母件表
                .addField("D.SpBm AS spBm")//商品编码

                .addLeftJoin("TCG_PurchaseAddress E ON E.Id = B.PurchaseAddressId ")//采购地址维护
                .addField("E.VGjH + ',' + E.VSfH + ',' + E.VDsH + ',' + E.VCqH + ',' + E.VDzH  AS addressDetail")//地址详情

                .addField("E.VGjH + ',' + E.VSfH + ',' + E.VDsH + ',' + E.VCqH + ',' + E.VDzH + ' '+A.PlanBid + ' '+ D.SpBm AS placeAddress")//采购下单地址

                .addLeftJoin("TP_ProductBasis I ON I.Code = D.SpBm ")//商品母件信息表
                .addField("I.Name AS spName")//商品名称


                .setBid(bid)
                .queryFirstMap();

        return extraParam;
    }


    public List<Map<String,Object>> getItemListByPid(Long pid){
        List<Map<String,Object>> sonList = new TcgPurchaseByOrderSon()

                /*
                .addField("H.SupplierId AS supplierId")//供应商ID
                .addField("H.SupplierName AS supplierName")//供应商名称
                .addField("H.Sum AS sum")//子件采购总数
                .addField("H.RealityPrice AS realityPrice")//子件实际单价
                .addField("H.RealityCost AS realityCost")//子件实际总价
                .addField("H.GoodsId AS goodsId")//1688商品采购ID
                .addField("H.SkuId AS skuId")//SKUID
                .addField("H.SpecId AS specId")//SPECID
                .addField("H.Sku AS sku")//SKU属性
                .addField("H.RealitySku AS realitySku")//实际SKU属性
                .addField("H.FrkSl AS frkSl")//入库数量

                 */

                .addLeftJoin("TCG_PurchaseByPlanSon J ON J.Id = A.PurchasePlanSonId ")//计划单子件信息表
                .addField("J.SubCode AS subCode")//子件编码
                .addField("J.Bprice AS bprice")//子件预估单价
                .addField("ISNULL((J.PurchaseAmount + J.JgSl) * J.Bprice,0) AS bpriceTotal")//子件预估总价

                .addLeftJoin("TP_ProductBasisComponent K ON K.Code = J.SubCode ")//商品子件信息表
                .addField("K.Name AS subName")//子件名称
                .addField("K.LinkIn AS linkIn")//子件链接


                .setPid(pid)
                .queryMap();


        return sonList;
    }


    /*
     * 采购完成物流修改
     */
    @GetMapping("/toUpdate")
    public String toUpdate(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            return commonApiService.toEdit(model,id, new TcgPurchaseByOrder(), MENU_ID1, null, null);
        }catch (Exception e) {
            logger.error("物流修改页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }
}






